<?php
class MTraining extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }
    public function addtouchbase(){
        
        $data = array( 
			'DateTouchBase' => db_clean($_POST['DateTouchBase']),
			'StaffID' => db_clean($_POST['StaffID']),
			'Location' => db_clean($_POST['Location']),
            'Course' => db_clean($_POST['Course']),
            'Duration' => db_clean($_POST['Duration']),
            'Purpose' => db_clean($_POST['Purpose']),
            'Comment' => db_clean($_POST['Comment'])  
		);
 	    $this->db->insert('touchbase', $data);
    }
    
    public function edittouchbase(){
        
        $data = array( 
			'DateTouchBase' => db_clean($_POST['DateTouchBase']),
			'StaffID' => db_clean($_POST['StaffID']),
			'Location' => db_clean($_POST['Location']),
            'Course' => db_clean($_POST['Course']),
            'Duration' => db_clean($_POST['Duration']),
            'Purpose' => db_clean($_POST['Purpose']),
            'Comment' => db_clean($_POST['Comment'])  
		);
        $this->db->where('TouchBaseID', db_clean($_POST['TouchBaseID']));
        $this->db->update('touchbase', $data); 
    }
    
    public function addtrainingclass(){
        
        $data = array( 
			'TCDate' => db_clean($_POST['TCDate']),
			'CourseID' => db_clean($_POST['CourseID']),
			'Duration' => db_clean($_POST['Duration']),
            'TrainingRoom' => db_clean($_POST['TrainingRoom']),
            'ParticipantM' => db_clean($_POST['ParticipantM']),
            'ParticipantF' => db_clean($_POST['ParticipantF']),
            'Evaluation' => db_clean($_POST['Evaluation']),  
            'Remark' => db_clean($_POST['Remark']), 
		);
 	    $this->db->insert('trainingclass', $data);
    }
    
    public function edittrainingclass(){
        
        $data = array( 
			'TCDate' => db_clean($_POST['TCDate']),
			'CourseID' => db_clean($_POST['CourseID']),
			'Duration' => db_clean($_POST['Duration']),
            'TrainingRoom' => db_clean($_POST['TrainingRoom']),
            'ParticipantM' => db_clean($_POST['ParticipantM']),
            'ParticipantF' => db_clean($_POST['ParticipantF']),
            'Evaluation' => db_clean($_POST['Evaluation']),  
            'Remark' => db_clean($_POST['Remark']), 
		);
        $this->db->where('TCID', db_clean($_POST['TCID']));
        $this->db->update('trainingclass', $data); 
    }
    
    public function getCourseOption(){
		$sql = "SELECT `CourseID`, `Topic` FROM `course`";
        $result =  $this->db->query($sql);
		$option = '<option value="">--- Select ---</option>';
		foreach($result->result() as $value){
		    $option .= '<option value="'.$value->CourseID.'">'.htmlspecialchars($value->Topic, ENT_QUOTES).'</option>';
		}
		return $option;
	}
    
    public function getCourseDropdown(){
		 $data = array();
		 $this->db->where('CActive',1);
	     $Q = $this->db->get('course');
	     if ($Q->num_rows() > 0){
	       $data[0] = '...select course...';
	       foreach ($Q->result_array() as $row){
	         $data[$row['CourseID']] = $row['Topic'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 	
	}
    
    public function geRecTrainingTypeOption(){
		$myopt = '<option value="">---Select----</option>';
		$myopt .= '<option value="0">Recieved</option>';
		$myopt .= '<option value="1">Planing</option>';
    	return $myopt;
    	
	} 
    public function getPlaningTimeOption(){
		$sql = "SELECT `QPID`,`QuaterPlan` FROM `quaterplan`";
        $result =  $this->db->query($sql);
		$option = '<option value="">--- Select ---</option>';
		foreach($result->result() as $value){
		    $option .= '<option value="'.$value->QPID.'">'.htmlspecialchars($value->QuaterPlan, ENT_QUOTES).'</option>';
		}
		return $option;
	}
     public function getLocationOption(){
		$sql = "SELECT `LocationID`,`Location` FROM `location`";
        $result =  $this->db->query($sql);
		$option = '<option value="">--- Select ---</option>';
		foreach($result->result() as $value){
		    $option .= '<option value="'.$value->LocationID.'">'.htmlspecialchars($value->Location, ENT_QUOTES).'</option>';
		}
		return $option;
	}
    
    public function getVisitTypeOption(){
		$myopt = '<option value="">---Select----</option>';
		$myopt .= '<option value="0">Visited</option>';
		$myopt .= '<option value="1">Planing</option>';
    	return $myopt;
    	
	} 
    
    public function getReceiveTrainingByStaffId($id)
    {
        $query = "SELECT sr.`RTID`,sr.`StaffID`, sr.`RTType`, sr.`DateRTraining`, c.`CourseID`, c.`Topic`, sr.`TrainingClass`, sr.`Duration`,sr.`Comment`  
                    FROM `staffrectraining` AS sr
                    INNER JOIN `staff` AS s ON s.`StaffID` = sr.`StaffID`
                    LEFT JOIN `quaterplan` AS qt ON qt.`QPID` = sr.`PlaningTime`
                    INNER JOIN `course` AS c ON c.`CourseID` = sr.`CourseID`
                    where sr.`StaffID`=".$id;
        $result =  $this->db->query($query);
        return $result;
    }
    
    public function getDesirableTrainingByStaffId($id)
    {
        $query = "SELECT sd.`ID`, sd.`StaffID`, c.`Topic`, c.`CourseID`,sd.`Comment`, sd.`DateTraining` FROM `staffdeserabletraining` AS sd
                    INNER JOIN staff AS s ON s.`StaffID` = sd.`StaffID`
                    INNER JOIN `course` AS c ON c.`CourseID` = sd.`CourseID`
                    where  sd.`StaffID`=".$id;
        $result =  $this->db->query($query);
        return $result;
    }
    
    public function getStaffSkillsByStaffId($id)
    {
        $query = "SELECT ss.`StaffSkillID`, ss.`StaffID`, ss.`CourseID`, c.`Topic`, ss.`Remark` FROM `staffskill` AS ss
                INNER JOIN staff AS s ON s.`StaffID` = ss.`StaffID`
                INNER JOIN `course` AS c ON c.`CourseID` = ss.`CourseID`
                    where ss.`StaffID`=".$id;
        $result =  $this->db->query($query);
        return $result;
    }
    
    public function getStaffFieldVisitByStaffId($id)
    {
        $query = "SELECT sf.`FieldVisitID`, sf.`StaffID`, sf.`VisitType`, l.`LocationID`, l.`Location`, sf.`DateFieldVisit`, qp.`QPID`, qp.`QuaterPlan`, sf.`Duration`, sf.`Purpose`, sf.`Comment` FROM `stafffieldvisit` AS sf
                INNER JOIN staff AS s ON s.`StaffID` = sf.`StaffID`
                INNER JOIN location AS l ON l.`LocationID` = sf.`LocationID`
                LEFT JOIN quaterplan AS qp ON qp.`QPID` = sf.`PlaningTime`
                    where sf.`StaffID`=".$id;
        $result =  $this->db->query($query);
        return $result;
    }
    
 }